package game.tile;

import javafx.scene.canvas.GraphicsContext;
import game.basic.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static game.basic.GameBase.*;
import static game.controller.GameObject.currentMap;
import static game.main.GameApp.player;

public class TileManager {

    String[] dataTile;//存储所有瓦片的数据信息

    public TileManager() {
        dataTile = new String[76];
        loadTileData("/maps/tiledata.txt");
        loadMap("/maps/worldmap.txt", 0);
        loadTileImage();
    }

    public void loadTileData(String url) {
        try (InputStream is = getClass().getResourceAsStream(url)) {
            if (is != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                for (int i = 0; i < 76; i++) {
                    String[] split = br.readLine().split("\n");
                    dataTile[i] = split[0];
                }
                br.close();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public void loadMap(String url, int i) {
        try (InputStream is = getClass().getResourceAsStream(url)) {
            if (is != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                for (int y = 0; y < maxWorldRow; y++) {
                    String[] split = br.readLine().split(" ");
                    for (int x = 0; x < maxWorldCol; x++) {
                        mapTileNum[i][y][x] = Integer.parseInt(split[x]);
                    }
                }
                br.close();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    private void loadTileImage() {
        for (int i = 0; i < 38; i++) {
            setTileImage(i, dataTile[i * 2], dataTile[i * 2 + 1].equals("true"));
        }
    }

    private void setTileImage(int index, String url, boolean collision) {
        tile[index] = new Tile();
        tile[index].image = Util.getImage("/tiles/" + url, tileSize, tileSize);
        tile[index].collisionTile = collision;
    }

    public void drawMap(GraphicsContext gc) {
        for (int y = 0; y < maxWorldRow; y++) {
            for (int x = 0; x < maxWorldCol; x++) {
                if ((x * tileSize) + tileSize > player.worldX - screenX &&
                        (x * tileSize) - tileSize < player.worldX + screenX &&
                        (y * tileSize) + tileSize > player.worldY - screenY &&
                        (y * tileSize) - tileSize < player.worldY + screenY) {

                    gc.drawImage(tile[mapTileNum[currentMap][y][x]].image,
                            (x * tileSize) - player.worldX + screenX,
                            (y * tileSize) - player.worldY + screenY);
                    //gc.strokeRect((x * tileSize) - player.worldX + screenX, (y * tileSize) - player.worldY + screenY, tileSize, tileSize);
                }
            }
        }
    }

}
